{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bsite import Bsite\n",
    "\n",
    "\n",
    "cookies = {\"cookie\": \"_uuid=110BFAF6-8F43-4250-685F-F95A1101385827328infoc; LIVE_BUVID=AUTO7415809758487425; im_notify_type_122592901=0; rpdid=|(k|mY|YRYR)0J'ul)u||||~l; UM_distinctid=171b3f4da444fb-02d7cdd3b1ca3c-30637600-1aeaa0-171b3f4da45d19; CNZZDATA1272960325=1961451073-1587857248-https%253A%252F%252Fmember.bilibili.com%252F%7C1589759255; blackside_state=1; sid=m639np6t; DedeUserID=122592901; DedeUserID__ckMd5=8ee93e9872d7be92; SESSDATA=c7198332%2C1614858098%2C28e98*91; bili_jct=c6a3b3237005136ca38cca7523e2dc70; CURRENT_FNVAL=80; buvid3=E60A736C-D5C9-43D1-BEA9-F93A4069B3F8143091infoc; finger=-169119620; CURRENT_QUALITY=80; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1600426052,1601293642,1602732801,1602737110; Hm_lpvt_8a6e55dbd2870f0f5bc9194cddf32a02=1602737110; bsource=search_baidu; PVID=2; bp_video_offset_122592901=446276449569232892; bp_t_offset_122592901=446279395914713929\"}\n",
    "bs = Bsite(cookies=cookies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. bvid与aid转化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44384851"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bs.bvid2aid(bvid='BV1eb411h7sP')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'BV1eb411h7sP'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bs.aid2bvid(aid=44384851)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 下载某用户所有视频信息\n",
    "获取**DJI大疆创新** https://space.bilibili.com/232472043/video 所有投稿视频相关信息。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该用户共有 439 个视频\n",
      "该用户相关视频信息请查看dji_videos.csv\n"
     ]
    }
   ],
   "source": [
    "bs.video_list(mid=232472043, csvfpath='dji_videos.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 获取某视频内的所有评论\n",
    "获取该视频 https://www.bilibili.com/video/BV1E54y1C7MF 所有的评论"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该视频共有 120 个评论\n"
     ]
    }
   ],
   "source": [
    "aid = bs.bvid2aid('BV1E54y1C7MF')\n",
    "bs.comments(aid=aid, csvfpath='comments.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>author</th>\n",
       "      <th>bvid</th>\n",
       "      <th>aid</th>\n",
       "      <th>mid</th>\n",
       "      <th>created</th>\n",
       "      <th>length</th>\n",
       "      <th>comment</th>\n",
       "      <th>description</th>\n",
       "      <th>pic</th>\n",
       "      <th>play</th>\n",
       "      <th>subtitle</th>\n",
       "      <th>title</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>DJI大疆创新</td>\n",
       "      <td>BV1E54y1C7MF</td>\n",
       "      <td>842473459</td>\n",
       "      <td>232472043</td>\n",
       "      <td>1602731125</td>\n",
       "      <td>01:00</td>\n",
       "      <td>120</td>\n",
       "      <td>DJI RSC 2 单机版售价2699元，套装版售价3999元，可于大疆天猫、京东、苏宁旗舰...</td>\n",
       "      <td>https://i0.hdslb.com/bfs/archive/9a18675673a62...</td>\n",
       "      <td>15189</td>\n",
       "      <td>NaN</td>\n",
       "      <td>大疆全新可折叠稳定器 - DJI RSC 2 现已就位</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>DJI大疆创新</td>\n",
       "      <td>BV1DA41177UY</td>\n",
       "      <td>329910105</td>\n",
       "      <td>232472043</td>\n",
       "      <td>1602730815</td>\n",
       "      <td>02:27</td>\n",
       "      <td>128</td>\n",
       "      <td>DJI RS 2 单机版售价4999元，套装版售价5999元，现可于大疆天猫、京东、苏宁旗舰...</td>\n",
       "      <td>https://i0.hdslb.com/bfs/archive/78b9f0d828a2b...</td>\n",
       "      <td>18461</td>\n",
       "      <td>NaN</td>\n",
       "      <td>大疆旗舰稳定器 - DJI RS 2 正式登场</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DJI大疆创新</td>\n",
       "      <td>BV19K4y1h72B</td>\n",
       "      <td>884978892</td>\n",
       "      <td>232472043</td>\n",
       "      <td>1602684334</td>\n",
       "      <td>03:14</td>\n",
       "      <td>496</td>\n",
       "      <td>禅思L1集成 Livox 激光雷达模块、高精度惯导、测绘相机、三轴云台等模块，与经纬 M30...</td>\n",
       "      <td>https://i0.hdslb.com/bfs/archive/ccfd26bc2ffbd...</td>\n",
       "      <td>66434</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DJI大疆发布禅思L1激光可见光融合负载，高效构建三维数字世界</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>DJI大疆创新</td>\n",
       "      <td>BV14a4y1L7rV</td>\n",
       "      <td>669946806</td>\n",
       "      <td>232472043</td>\n",
       "      <td>1602684029</td>\n",
       "      <td>03:12</td>\n",
       "      <td>470</td>\n",
       "      <td>大疆行业应用的首款全画幅传感器相机负载 —— 禅思P1，像素高达4500万，采用全局机械快门...</td>\n",
       "      <td>https://i0.hdslb.com/bfs/archive/656161cc217fd...</td>\n",
       "      <td>59834</td>\n",
       "      <td>NaN</td>\n",
       "      <td>DJI大疆发布禅思P1全画幅相机负载，实现航空测绘新突破</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>DJI大疆创新</td>\n",
       "      <td>BV15T4y1c7op</td>\n",
       "      <td>927207847</td>\n",
       "      <td>232472043</td>\n",
       "      <td>1601201439</td>\n",
       "      <td>01:12</td>\n",
       "      <td>36</td>\n",
       "      <td>大疆创作者Angelo Meloni  的这天似乎过得不太顺，但不用担心，万能的奶奶和 DJ...</td>\n",
       "      <td>https://i0.hdslb.com/bfs/archive/900f51e805c86...</td>\n",
       "      <td>6025</td>\n",
       "      <td>NaN</td>\n",
       "      <td>【大疆DJI OM 4】老奶奶的美食vlog</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    author          bvid        aid        mid     created length  comment  \\\n",
       "0  DJI大疆创新  BV1E54y1C7MF  842473459  232472043  1602731125  01:00      120   \n",
       "1  DJI大疆创新  BV1DA41177UY  329910105  232472043  1602730815  02:27      128   \n",
       "2  DJI大疆创新  BV19K4y1h72B  884978892  232472043  1602684334  03:14      496   \n",
       "3  DJI大疆创新  BV14a4y1L7rV  669946806  232472043  1602684029  03:12      470   \n",
       "4  DJI大疆创新  BV15T4y1c7op  927207847  232472043  1601201439  01:12       36   \n",
       "\n",
       "                                         description  \\\n",
       "0  DJI RSC 2 单机版售价2699元，套装版售价3999元，可于大疆天猫、京东、苏宁旗舰...   \n",
       "1  DJI RS 2 单机版售价4999元，套装版售价5999元，现可于大疆天猫、京东、苏宁旗舰...   \n",
       "2  禅思L1集成 Livox 激光雷达模块、高精度惯导、测绘相机、三轴云台等模块，与经纬 M30...   \n",
       "3  大疆行业应用的首款全画幅传感器相机负载 —— 禅思P1，像素高达4500万，采用全局机械快门...   \n",
       "4  大疆创作者Angelo Meloni  的这天似乎过得不太顺，但不用担心，万能的奶奶和 DJ...   \n",
       "\n",
       "                                                 pic   play  subtitle  \\\n",
       "0  https://i0.hdslb.com/bfs/archive/9a18675673a62...  15189       NaN   \n",
       "1  https://i0.hdslb.com/bfs/archive/78b9f0d828a2b...  18461       NaN   \n",
       "2  https://i0.hdslb.com/bfs/archive/ccfd26bc2ffbd...  66434       NaN   \n",
       "3  https://i0.hdslb.com/bfs/archive/656161cc217fd...  59834       NaN   \n",
       "4  https://i0.hdslb.com/bfs/archive/900f51e805c86...   6025       NaN   \n",
       "\n",
       "                             title  \n",
       "0      大疆全新可折叠稳定器 - DJI RSC 2 现已就位  \n",
       "1          大疆旗舰稳定器 - DJI RS 2 正式登场  \n",
       "2  DJI大疆发布禅思L1激光可见光融合负载，高效构建三维数字世界  \n",
       "3     DJI大疆发布禅思P1全画幅相机负载，实现航空测绘新突破  \n",
       "4           【大疆DJI OM 4】老奶奶的美食vlog  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "vlist = pd.read_csv('dji_videos.csv')\n",
    "vlist.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>content</th>\n",
       "      <th>device</th>\n",
       "      <th>like</th>\n",
       "      <th>rcount</th>\n",
       "      <th>ctime</th>\n",
       "      <th>avatar</th>\n",
       "      <th>level</th>\n",
       "      <th>sex</th>\n",
       "      <th>sign</th>\n",
       "      <th>uname</th>\n",
       "      <th>mid</th>\n",
       "      <th>oid</th>\n",
       "      <th>diag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>dji，yes！</td>\n",
       "      <td>NaN</td>\n",
       "      <td>59</td>\n",
       "      <td>5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>http://i2.hdslb.com/bfs/face/c1bfc931e523c78e7...</td>\n",
       "      <td>4</td>\n",
       "      <td>保密</td>\n",
       "      <td>诞生于抗日烽火之中，崛起于民族复兴之时。\\n静思笃行，持中秉正。\\n\\n投稿邮箱：12136...</td>\n",
       "      <td>江西师范大学</td>\n",
       "      <td>487631896</td>\n",
       "      <td>842473459</td>\n",
       "      <td>原评论</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>公号私用？</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>http://i0.hdslb.com/bfs/face/ef80bde4f75a819ad...</td>\n",
       "      <td>4</td>\n",
       "      <td>保密</td>\n",
       "      <td>NaN</td>\n",
       "      <td>若月枫</td>\n",
       "      <td>274910604</td>\n",
       "      <td>842473459</td>\n",
       "      <td>原评论</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>江西师范惊了[doge]</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>http://i1.hdslb.com/bfs/face/27c3f49ab5cd8cfd6...</td>\n",
       "      <td>4</td>\n",
       "      <td>保密</td>\n",
       "      <td>西瓜，爱好摄影  ins：zwx598890236</td>\n",
       "      <td>CaptainGoodguy</td>\n",
       "      <td>2399917</td>\n",
       "      <td>842473459</td>\n",
       "      <td>原评论</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>DJI YYDS</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>http://i1.hdslb.com/bfs/face/9df780ceb56bd4dd5...</td>\n",
       "      <td>5</td>\n",
       "      <td>保密</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nothingllam</td>\n",
       "      <td>267387088</td>\n",
       "      <td>842473459</td>\n",
       "      <td>原评论</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SC可以加钱升级吗</td>\n",
       "      <td>NaN</td>\n",
       "      <td>29</td>\n",
       "      <td>11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>http://i1.hdslb.com/bfs/face/a6d82fc35a45adfd4...</td>\n",
       "      <td>5</td>\n",
       "      <td>男</td>\n",
       "      <td>一名朴实无华且枯燥的数码科技狗</td>\n",
       "      <td>籽籽爱分享</td>\n",
       "      <td>321981297</td>\n",
       "      <td>842473459</td>\n",
       "      <td>原评论</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        content device  like  rcount  ctime  \\\n",
       "0      dji，yes！    NaN    59       5    NaN   \n",
       "1         公号私用？    NaN     3       0    NaN   \n",
       "2  江西师范惊了[doge]    NaN     0       0    NaN   \n",
       "3      DJI YYDS    NaN     0       0    NaN   \n",
       "4     SC可以加钱升级吗    NaN    29      11    NaN   \n",
       "\n",
       "                                              avatar  level sex  \\\n",
       "0  http://i2.hdslb.com/bfs/face/c1bfc931e523c78e7...      4  保密   \n",
       "1  http://i0.hdslb.com/bfs/face/ef80bde4f75a819ad...      4  保密   \n",
       "2  http://i1.hdslb.com/bfs/face/27c3f49ab5cd8cfd6...      4  保密   \n",
       "3  http://i1.hdslb.com/bfs/face/9df780ceb56bd4dd5...      5  保密   \n",
       "4  http://i1.hdslb.com/bfs/face/a6d82fc35a45adfd4...      5   男   \n",
       "\n",
       "                                                sign           uname  \\\n",
       "0  诞生于抗日烽火之中，崛起于民族复兴之时。\\n静思笃行，持中秉正。\\n\\n投稿邮箱：12136...          江西师范大学   \n",
       "1                                                NaN             若月枫   \n",
       "2                          西瓜，爱好摄影  ins：zwx598890236  CaptainGoodguy   \n",
       "3                                                NaN     nothingllam   \n",
       "4                                    一名朴实无华且枯燥的数码科技狗           籽籽爱分享   \n",
       "\n",
       "         mid        oid diag  \n",
       "0  487631896  842473459  原评论  \n",
       "1  274910604  842473459  原评论  \n",
       "2    2399917  842473459  原评论  \n",
       "3  267387088  842473459  原评论  \n",
       "4  321981297  842473459  原评论  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments = pd.read_csv('comments.csv')\n",
    "comments.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
